﻿using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Control;
using Super_Management_Mayhem.Screens.PlayerSelection;
using Super_Management_Mayhem.Screens.Playing.UI;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Input;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.GameRoot
{
    public class SelectPlayers : Container
    {
        private Button[] _buttons;
        private int _currentButtonSelected;

        private InputKeys _firstPlayerInput;

        public enum PlayerCount
        {
            Two,
            Three,
            Four,
            Five,
            Six,
            Seven,
            Eight,
        }

        public PlayerCount Players
        {
            get
            {
                return (PlayerCount) _currentButtonSelected;
            }
        }

        public override void LoadContent()
        {
            MenuSheet.LoadContent();

            _firstPlayerInput = InputKeys.FromFile("Content/Config/Input/Player1.input");
            KeyboardHandler.OnKeyboardPress += Keypress;

            var buttonContainer = new Container(3, 3);

            _currentButtonSelected = 0;

            _buttons = new[] {
                                 new Button(new TE_Vector2(0, 0), "Two players"),
                                 new Button(new TE_Vector2(0, 3), "Three players"),
                                 new Button(new TE_Vector2(0, 6), "Four players"),
                                 new Button(new TE_Vector2(0, 9), "Six players"),
                                 new Button(new TE_Vector2(0, 12), "Eight players"),
                             };

            _buttons[0].Select();

            buttonContainer.AddChildren(_buttons);

            AddChildren(new OuterBorder(), buttonContainer);

            base.LoadContent();
        }

        void MoveSelectionUp()
        {
            if(_currentButtonSelected > 0)
            {
                _buttons[_currentButtonSelected].Unselect();
                _currentButtonSelected--;
                _buttons[_currentButtonSelected].Select();
            }
        }

        void MoveSelectionDown()
        {
            if(_currentButtonSelected < _buttons.Length - 1)
            {
                _buttons[_currentButtonSelected].Unselect();
                _currentButtonSelected++;
                _buttons[_currentButtonSelected].Select();
            }
        }

        private void Keypress(KeyboardInput input)
        {
            if(input.KeyCode == _firstPlayerInput.UpKey)
            {
                MoveSelectionUp();
            }
            else if(input.KeyCode == _firstPlayerInput.DownKey)
            {
                MoveSelectionDown();
            }
            else if(input.KeyCode == _firstPlayerInput.AttackKey)
            {
                //Select number of players, change screen
                Game.Close();
            }
        }

        public override void UnloadContent()
        {
            KeyboardHandler.OnKeyboardPress -= Keypress;
            base.UnloadContent();
        }
    }
}
